//
//  DDUserInfoService.m
//  DingDongZP
//
//  Created by Ray on 16/4/12.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDUserInfoService.h"
#import "DDLoginUserManager.h"

@implementation DDUserInfoService

- (void)queryUserInfo:(void (^)(DDUserInfoModel *))callback
{
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_query_userinfo"];
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:nil success:^(id responseObject) {
        NSInteger success = [responseObject[@"code"] integerValue];
        if (success) {
            NSDictionary *data = responseObject[@"data"];
            if (data) {
                DDUserInfoModel *userinfo = [DDUserInfoModel yy_modelWithJSON:data];
                if (callback) {
                    callback(userinfo);
                    return; //  skip after.
                }
            }
        }
        callback(nil);
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];
}

- (void)queryUserInfoWithFresh:(BOOL)fresh callback:(void (^)(DDUserInfoModel *))callback
{
    NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:@"kUserInfo"];
    DDUserInfoModel *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    if (user && !fresh) {
        callback(user);
    } else {
        [self queryUserInfo:^(DDUserInfoModel *model) {
            callback(model);
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"kUserInfo"];
        }];
    }
}

- (void)updateName:(NSString *)name callback:(void (^)(BOOL succ))callback
{
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_setname"];
    
    NSDictionary *params = @{@"name": name}.mutableCopy;
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:params success:^(id responseObject) {
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
}
- (void)updateSex:(NSInteger)name callback:(void (^)(BOOL succ))callback
{
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_setsex"];
    
    NSDictionary *params = @{@"sex": @(name)}.mutableCopy;
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:params success:^(id responseObject) {
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
}
- (void)updateWorkyear:(NSString*)name callback:(void (^)(BOOL succ))callback
{
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_setworkyear"];
    
    NSDictionary *params = @{@"exp": name}.mutableCopy;
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:params success:^(id responseObject) {
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
}
- (void)updateWxNumber:(NSString *)name callback:(void (^)(BOOL succ))callback
{
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_setweixin"];
    
    NSDictionary *params = @{@"weixin": name}.mutableCopy;
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:params success:^(id responseObject) {
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
}

- (void)uploadLogo:(UIImage *)logo callback:(void (^)(BOOL succ))callback {
    
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_upload_logo"];
    
    NSDictionary *params = @{@"file": logo}.mutableCopy;
    [[DDNetWorkManager shareManager] UPLOAD:requestUrlString parameters:nil filesDatas:params success:^(id responseObject) {
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
    
}
+ (void)uploadLogo:(UIImage *)logo callback:(void (^)(BOOL))callback {
    
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_upload_logo"];
    
    NSDictionary *params = @{@"file": logo}.mutableCopy;
    [[DDNetWorkManager shareManager] UPLOAD:requestUrlString parameters:nil filesDatas:params success:^(id responseObject) {
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
}

+ (instancetype)sharedService {
    static DDUserInfoService *__service;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __service = [[DDUserInfoService alloc] init];
    });
    return __service;
}

- (void)addActivityWithType:(NSString *)activeType callback:(void (^)(BOOL, id))callback
{
    if (!activeType) {
        callback(NO, @"type不能为空");
    }
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"jobfinder_add_activeness"];
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:@{@"type": activeType} success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSNumber *newActive = responseObject[@"data"];
            callback(YES, newActive);
        } else {
            NSString *err = responseObject[@"msg"];
            callback(NO, err);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(NO, error.domain);
    }];
}

@end
